<?php
/**
 * Created by Sublime.
 * User: chindor
 * Date: 2017/4/21
 * Time: 17:11
 */

namespace app\api\modules\v1\market\controllers;


use app\api\components\ApiController;
use app\api\modules\v1\market\models\Message;
use Yii;
use app\api\modules\v1\market\models\BlackBoard;
use app\api\modules\v1\market\models\Operate;
use app\api\modules\v1\market\models\AppUser;
use app\models\base\Activity;

class OperateController extends ApiController
{
	public $maxsize = 99999999000000;

    public $img_extension = [
        'bmp','jpeg','jpg','gif','png'
    ];

    public $doc_extension = [
        'doc','docx','xls','xlsx'
    ];

    public $default_extension = [
        'png','bmp','jpeg','jpg','gif','doc','docx','xls','xlsx'
    ];
	/**
	 * 新建活动指派给自己
	 * 指派
	 */
	public function actionAssign()
	{
		$session = Yii::$app->session;
		$post = Yii::$app->request->post();
		$files = $_FILES;

		//验证token
        $verifyToken = new AppUser();
        if($verifyToken->verifyAuth($post['create_user_id'],$post['token']) == false) return parent::sendMessageJson('用户认证不通过');
        //判断图片或者附件大小类型是否符合要求
        if(!$this->validateFile($files)){
        	return parent::sendMessageJson($session->getFlash('error'));
        }

        //指派
		$data = Operate::assign($post,$files);
		if($data){
			return parent::sendMessageJson('指派成功',self::$CODE_SUC);
		}else{
			return parent::sendMessageJson('指派失败');
		}
	}


	/**
	 * 新建活动指派给自己
	 * 评论
	 * @return [array] [成功 or 失败]
	 */
	public function actionComment()
	{
		$session = Yii::$app->session;
		$post = Yii::$app->request->post();
		$files = $_FILES;
		$activity_id = $post['activity_id'];

		//验证token
        $verifyToken = new AppUser();
        if($verifyToken->verifyAuth($post['create_user_id'],$post['token']) == false) return parent::sendMessageJson('用户认证不通过');
        if(empty($activity_id)){
        	return parent::sendMessageJson('活动id不能为空');
        }

        //判断图片或者附件大小类型是否符合要求
        if(!$this->validateFile($files)){
        	return parent::sendMessageJson($session->getFlash('error'));
        }

		$data = Operate::comment($post,$files);
		if($data){
			return parent::sendMessageJson('评论成功',self::$CODE_SUC);
		}else{
			return parent::sendMessageJson('评论失败');
		}
	}

	/**
	 * 新建活动指派给自己
	 * 删除
	 * @return [array] [成功 or 失败]
	 */
	public function actionDelete()
	{
		$post = Yii::$app->request->post();
		$activity_id = $post['activity_id'];

		//验证token
        $verifyToken = new AppUser();
        if($verifyToken->verifyAuth($post['create_user_id'],$post['token']) == false) return parent::sendMessageJson('用户认证不通过');
        if(empty($activity_id)){
        	return parent::sendMessageJson('活动id不能为空');
        }

        $activity = Activity::find()->where(['parent_id'=>$activity_id])->limit(1)->one();
        if($activity){
        	return parent::sendMessageJson('存在下属活动,删除失败');
        }

		//把价值还给项目或者父级活动并删除活动
		$res = Operate::delete($activity_id);

		if($res){
			return parent::sendMessageJson('删除成功',self::$CODE_SUC);
		}else{
			return parent::sendMessageJson('删除失败');
		}
		
	}

	/**
	 * [接受指派的活动]
	 * @return [type] [description]
	 */
	public function actionAccept()
	{
		$session = Yii::$app->session;
		$post = Yii::$app->request->post();
		$files = $_FILES;
		$activity_id = $post['activity_id'];

		//验证token
        $verifyToken = new AppUser();
        if($verifyToken->verifyAuth($post['create_user_id'],$post['token']) == false) return parent::sendMessageJson('用户认证不通过');
        if(empty($activity_id)){
        	return parent::sendMessageJson('活动id不能为空');
        }
        //判断图片或者附件大小类型是否符合要求
        if(!$this->validateFile($files)){
        	return parent::sendMessageJson($session->getFlash('error'));
        }
        // var_dump($files);exit;
        //接受活动
        $res = Operate::accept($post,$files);
        if($res){
			return parent::sendMessageJson('接受成功',self::$CODE_SUC);
		}else{
			return parent::sendMessageJson('接受失败');
		}
	}

	/**
	 * [拒绝指派的活动]
	 * @param  [$post]  [接收的数据]
	 * @return [array] [返回的数组]
	 */
	public function actionRefuse()
	{
		$session = Yii::$app->session;
		$post = Yii::$app->request->post();
		$files = $_FILES;
		$activity_id = $post['activity_id'];

		//验证token
        $verifyToken = new AppUser();
        if($verifyToken->verifyAuth($post['create_user_id'],$post['token']) == false) return parent::sendMessageJson('用户认证不通过');
        if(empty($activity_id)){
        	return parent::sendMessageJson('活动id不能为空');
        }

        //判断图片或者附件大小类型是否符合要求
        if(!$this->validateFile($files)){
        	return parent::sendMessageJson($session->getFlash('error'));
        }

        //拒绝活动
        $res = Operate::refuse($post,$files);
        if($res){
			return parent::sendMessageJson('拒绝成功',self::$CODE_SUC);
		}else{
			return parent::sendMessageJson('拒绝失败');
		}

	}

	/**
	 * [开始指派的活动]
	 * @param  [$post]  [接收的数据]
	 * @return [array] [返回的数组]
	 */
	public function actionBegin()
	{
		$session = Yii::$app->session;
		$post = Yii::$app->request->post();
		$files = $_FILES;
		$activity_id = $post['activity_id'];

		//验证token
        $verifyToken = new AppUser();
        if($verifyToken->verifyAuth($post['create_user_id'],$post['token']) == false) return parent::sendMessageJson('用户认证不通过');
        if(empty($activity_id)){
        	return parent::sendMessageJson('活动id不能为空');
        }

        //判断图片或者附件大小类型是否符合要求
        if(!$this->validateFile($files)){
        	return parent::sendMessageJson($session->getFlash('error'));
        }

        //开始活动
        $res = Operate::begin($post,$files);
        if($res){
			return parent::sendMessageJson('开始成功',self::$CODE_SUC);
		}else{
			return parent::sendMessageJson('开始失败');
		}
	}


	/**
	 * [阻塞指派的活动]
	 * @param  [$post]  [接收的数据]
	 * @return [array] [返回的数组]
	 */
	public function actionBlock()
	{
		$session = Yii::$app->session;
		$post = Yii::$app->request->post();
		$files = $_FILES;
		$activity_id = $post['activity_id'];

		//验证token
        $verifyToken = new AppUser();
        if($verifyToken->verifyAuth($post['create_user_id'],$post['token']) == false) return parent::sendMessageJson('用户认证不通过');
        if(empty($activity_id)){
        	return parent::sendMessageJson('活动id不能为空');
        }

        //判断图片或者附件大小类型是否符合要求
        if(!$this->validateFile($files)){
        	return parent::sendMessageJson($session->getFlash('error'));
        }

        //开始活动
        return $res = Operate::block($post,$files);
	}

	/**
	 * [取消阻塞指派的活动]
	 * @param  [$post]  [接收的数据]
	 * @return [array] [返回的数组]
	 */
	public function actionCancelBlock()
	{
		$session = Yii::$app->session;
		$post = Yii::$app->request->post();
		$files = $_FILES;
		$activity_id = $post['activity_id'];

		//验证token
        $verifyToken = new AppUser();
        if($verifyToken->verifyAuth($post['create_user_id'],$post['token']) == false) return parent::sendMessageJson('用户认证不通过');
        if(empty($activity_id)){
        	return parent::sendMessageJson('活动id不能为空');
        }

        //判断图片或者附件大小类型是否符合要求
        if(!$this->validateFile($files)){
        	return parent::sendMessageJson($session->getFlash('error'));
        }

        //开始活动
        return $res = Operate::cancelblock($post,$files);
	}

	/**
	 * [完成活动]
	 * @return [type] [description]
	 */
	public function actionComplete()
	{
		$session = Yii::$app->session;
		$post = Yii::$app->request->post();
		$files = $_FILES;
		$activity_id = $post['activity_id'];

		//验证token
        $verifyToken = new AppUser();
        if($verifyToken->verifyAuth($post['create_user_id'],$post['token']) == false) return parent::sendMessageJson('用户认证不通过');
        if(empty($activity_id)){
        	return parent::sendMessageJson('活动id不能为空');
        }

        //判断图片或者附件大小类型是否符合要求
        if(!$this->validateFile($files)){
        	return parent::sendMessageJson($session->getFlash('error'));
        }

        $res = Operate::complete($post,$files);
        return $res;
	}

	/**
	 * [审核不通过]
	 * @return [type] [description]
	 */
	public function actionReviewBack()
	{
		$session = Yii::$app->session;
		$post = Yii::$app->request->post();
		$files = $_FILES;
		$activity_id = $post['activity_id'];

		//验证token
        $verifyToken = new AppUser();
        if($verifyToken->verifyAuth($post['create_user_id'],$post['token']) == false) return parent::sendMessageJson('用户认证不通过');
        if(empty($activity_id)){
        	return parent::sendMessageJson('活动id不能为空');
        }

        //判断图片或者附件大小类型是否符合要求
        if(!$this->validateFile($files)){
        	return parent::sendMessageJson($session->getFlash('error'));
        }

        return $res = Operate::reviewback($post,$files);

	}

	/**
	 * [添加已消耗]
	 * @return [array] [description]
	 */
	public function actionCost()
	{
		$post = Yii::$app->request->post();
		//验证token
        $verifyToken = new AppUser();
        if($verifyToken->verifyAuth($post['create_user_id'],$post['token']) == false) return parent::sendMessageJson('用户认证不通过');
        if(empty($post['activity_id'])){
        	return parent::sendMessageJson('活动id不能为空');
        }


        return $res = Operate::addcost($post);
	}

	/**
	 * [添加进度]
	 * @return [array] [description]
	 */
	public function actionPercent()
	{
		$post = Yii::$app->request->post();
		//验证token
        $verifyToken = new AppUser();
        if($verifyToken->verifyAuth($post['create_user_id'],$post['token']) == false) return parent::sendMessageJson('用户认证不通过');
        if(empty($post['activity_id'])){
        	return parent::sendMessageJson('活动id不能为空');
        }


        return $res = Operate::percent($post);
	}


	/**
	 * [validateFile 验证上传的图片附件]
	 * @param  [type] $files [上传的文件]
	 * @return [type]        [返回的错误信息]
	 */
	public function validateFile($files)
	{
		// var_dump($files);exit;
		$session = \yii::$app->session;
		//如果上传了图片
		if(isset($files['image'])){
			foreach($files['image']['name'] as $k => $v){
				
				if($files['image']['size'][$k] > $this->maxsize){
					$session->setFlash('error','图片不能大于1000M');
		        	return false;
		        }
		        $type = substr($v,(strripos($v,'.')+1));
		        if(!in_array($type,$this->img_extension)){
		        	$session->setFlash('error','不支持该图片格式');
		        	return false;
		        }
		
			}
		}
		
		
		//如果上传了附件
		// if(isset($files['file'])){	
	 //        if($files['file']['size'] > $this->maxsize){
	 //        	$session->setFlash('error','附件不能大于1M');
	 //        	return false;
	 //        }
	 //        $type = substr($files['file']['name'],(strripos($files['file']['name'],'.')+1));
	 //        if(!in_array($type,$this->doc_extension)){
	 //        	$session->setFlash('error','不支持该附件格式');
	 //        	return false;
	 //        }
		// }
        return true;
	}

}